﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using pub.Functions;
using pub.Interop;
using scheduler.Model;

namespace scheduler
{
    public partial class NotificationForm : Form
    {

        public NotificationForm()
        {
            InitializeComponent();

//            this.Font = SystemFonts.IconTitleFont;
        }

        public TodoInfo TodoInfo { get; set; }

        private void NotificationForm_Load(object sender, EventArgs e)
        {
            lblTitle.Text = TodoInfo.Title;
            if (TodoInfo.PathTitles.Length > 1)
            {
                var sb = new StringBuilder();
                for (var n = 0; n < TodoInfo.PathTitles.Length; n++)
                {
                    sb.AppendLine(new string(' ', n * 2) + TodoInfo.PathTitles[n]);
                }
                this.toolTip1.SetToolTip(this.lblTitle, sb.ToString());
            }

            lblDue.Text = TodoInfo.Due;
            lblFile.Text = PathFuncs.CompactPath(TodoInfo.File, 49);
            lblDescription.Text = TodoInfo.Description;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            var pathTitlesStr = string.Join("\n", TodoInfo.PathTitles);
            var hwnd = CommonUtils.FindTodoWindowForTodoList(TodoInfo.File);
            if (hwnd != IntPtr.Zero)
            {
                CommonUtils.ActivateWindow(hwnd);

                var cds = new Win32.COPYDATASTRUCT
                {
                    dwData = (IntPtr)Consts.COPY_DATA_TYPE_ACTIVE_TODO,
                    cbData = pathTitlesStr.GetByteCount() + 1,
                    lpData = pathTitlesStr
                };
                Win32.SendMessage(hwnd, Win32.WM_COPYDATA, 0, ref cds);
            }
            else
            {
                var bts = Encoding.Default.GetBytes(pathTitlesStr);
                var pathTitlesEncoded = Convert.ToBase64String(bts);
                Process.Start("todo.exe", "\"" + TodoInfo.File + "\" " +
                                          "/activeTodo \"" + pathTitlesEncoded + "\"");
            }

            this.Close();
        }

        private void NotificationForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //
        }
    }
}